begintownscript;

variables;
short creature_count = 0;
short choice = 0;
short pc_counter = 0;

body;

beginstate 0; //INIT_STATE
		if (get_flag(10,0) != 1) {
		message_dialog("You enter the innermost area of the Temple of Flame, and find that it's really not that much. The only buildings here appear to be barracks, though the giant smoking pillar does catch your attention.","Brother Oakleaf runs ahead of you, clearly agitated. _The holding cells must be around here somewhere..._");
		set_flag(10,0,1);
		}
break;

beginstate 1; //EXIT_STATE

break;

beginstate 2; //START_STATE

break;

beginstate 10; //block off barracks
	block_entry(1);
	reset_dialog();
	message_dialog("This appears to be one of the Order of Flame's many barracks, and thus not pertinent to your mission. Countless locks line the doors, and claw marks cover the doorframes.","Needless to say, it'd be far too risky to break into the barracks, so you leave the heavily padlocked doors alone and return to seeking out Kharprev.");

break;

beginstate 15; //cutscene on to Kharprev
	//approach Oakleaf
	reset_dialog();
	message_dialog("You approach Brother Oakleaf cautiously, recognizing the anger that's driving him right now. Eventually, he notices your presence.","");
	pause(5);
	march_party(32,44);
	force_view_center(32,44);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(5);
	march_party(32,43);
	force_view_center(32,43);
	force_instant_terrain_redraw();
	play_sound(49);

	//Oakleaf's spiel
	pause(20);
	set_character_facing(7,4);
	force_instant_terrain_redraw();
	pause(10);
	text_bubble_on_char(7, "Ephesos is here.");
	force_instant_terrain_redraw();
	pause(15);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	text_bubble_on_char(7, "I feel his presence.");
	force_instant_terrain_redraw();
	pause(20);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	text_bubble_on_char(char_on_loc(32,43), "What now?");
	force_instant_terrain_redraw();
	pause(15);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	reset_dialog();
	message_dialog("Brother Oakleaf remains silent. You hear the bellow of a dragon - presumably Kharprev - to the north, and Oakleaf suddenly starts muttering a spell.","When his eyes glow blue, you decide it may not be so smart to interrupt him.");

	//door-blaster
	pause(10);
	set_character_pose(7,1);
	force_instant_terrain_redraw();
	pause(5);
	put_effect_on_char(7,0,4,1);
	run_animation_sound(11);
	pause(4);
	put_effect_on_char(7,0,7,1);
	run_animation_sound(11);
	pause(3);
	put_effect_on_char(7,0,10,1);
	run_animation_sound(11);
	pause(5);
	put_jagged_zap(32,41,29,40,1);
	put_jagged_zap(32,41,29,41,1);
	put_jagged_zap(32,41,29,42,1);
	put_jagged_zap(32,41,35,40,1);
	put_jagged_zap(32,41,35,41,1);
	put_jagged_zap(32,41,35,42,1);
	run_animation_sound(51);
	pause(5);
	set_character_pose(7,0);
	force_instant_terrain_redraw();
	pause(10);
	text_bubble_on_char(7, "That should blow the locks.");
	force_instant_terrain_redraw();
	pause(20);
	erase_text_bubbles();
	force_instant_terrain_redraw();

	//instructions
	reset_dialog();
	add_dialog_str(0, "The icy blue leaves Oakleaf's eyes, and he begins to speak again. _I'll check the cells, you distract Kharprev... once I free Ephesos, we can leave._",0);
	add_dialog_str(1, "Distraction?! You wish he was kidding, but the look on Oakleaf's face is all business.",0);
	add_dialog_str(2, "You realize that to save Ephesos (and the Order), you need to buy Oakleaf some time, even if it means putting yourselves directly in the line of fire...",0);
	add_dialog_str(3, "After a second, you hit yourself for even thinking of such a weak pun.",0);
	run_dialog(1);
	pause(25);
	text_bubble_on_char(char_on_loc(32,43), "We understand.");
	force_instant_terrain_redraw();
	pause(15);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	text_bubble_on_char(7, "Good. I wish you luck.");
	force_instant_terrain_redraw();
	pause(20);

	//leave, march up to Kharprev
	erase_text_bubbles();
	force_instant_terrain_redraw();
	pause(5);
	march_party(31,42);
	force_view_center(31,42);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(5);
	march_party(31,41);
	force_view_center(31,41);
	set_character_facing(7,2);
	force_instant_terrain_redraw();
	play_sound(49);
	pause(5);
	march_party(31,40);
	force_view_center(31,40);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(5);
	march_party(32,39);
	force_view_center(32,39);
	set_character_facing(7,0);
	force_instant_terrain_redraw();
	play_sound(49);
	pause(5);
	march_party(32,38);
	force_view_center(32,38);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(5);
	march_party(32,37);
	force_view_center(32,37);
	force_instant_terrain_redraw();
	play_sound(49);
	pause(5);
	march_party(32,36);
	force_view_center(32,36);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(15);
	
	//last advice
	reset_dialog();
	message_dialog("You reach the top of the stairs and pause at the entrance to the smoking pillar at the Temple's center. The inside of the pillar is completely filled with lava, and the heat is beyond oppressive.","A giant... thing... carved out of blackened stone rises out of the lava, and you guess that's where Kharprev nests.");
	message_dialog("You look back, and Oakleaf wishes you luck one last time. _Use the Skyshard to follow him if you must. I will work quickly, so you shouldn't have to last long._","This is small consolation when you see Kharprev high above you... craning your necks upward, you see him descend upon the blackened stone in a cloud of flame.");

	//enter Kharprev
	pause(5);
	force_view_center(32,30);
	force_instant_terrain_redraw();
	pause(5);
	put_effect_on_space(32,30,1,15,0);
	run_animation_sound(10);
	activate_hidden_group(10);
	force_instant_terrain_redraw();
	pause(15);
	text_bubble_on_char(6, "WHAT IS THIS?!");
	force_instant_terrain_redraw();
	pause(15);
	erase_text_bubbles();
	erase_char(7);
	force_instant_terrain_redraw();
	
	reset_dialog();
	add_dialog_str(0,"Kharprev's voice shakes every bone in your body, and you can't help but feel some awe for this beast. You still could never bring yourself to respect this foul creature, but that's a given.",0);
	add_dialog_str(1, "Kharprev stares down at you and laughs, a sound which brings some chunks of rock tumbling down from the walls.",0);
	add_dialog_str(2, "_WHAT MORTALS DARE DISTURB ME?!_ Kharprev bellows when he finishes laughing.", 0);
	add_dialog_str(3, "You silently curse Brother Oakleaf's plan. You didn't know Kharprev was quite this big... or this intimidating. As you stare down Kharprev, Brother Oakleaf departs to search the east holding cells.",0);
	run_dialog(1);

	//start the distraction
	pause(5);
	force_view_center(32,36);
	force_instant_terrain_redraw();
	pause(5);
	reset_dialog();
	message_dialog("Not knowing what else to do, you approach Kharprev to start distracting him. Of course, you're not sure exactly how to distract a 20-ton dragon.","Out of ideas, you decide to try something incredibly desperate...");	
	pause(15);
	force_instant_terrain_redraw();
	pause(5);
	march_party(32,35);
	force_view_center(32,35);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(5);
	march_party(32,34);
	force_view_center(32,34);
	force_instant_terrain_redraw();
	play_sound(49);
	pause(5);
	march_party(32,33);
	force_view_center(32,33);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(10);
	force_view_center(27,28);
	force_instant_terrain_redraw();
	pause(5);
	if(char_on_loc(32,35) != 1) {
		text_bubble_on_char(char_on_loc(32,35), "I hope this works...");
		}
	else {
		text_bubble_on_char(char_on_loc(32,33), "I hope this works...");
		}
	force_instant_terrain_redraw();
	pause(20);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	text_bubble_on_char(char_on_loc(32,33), "Knock knock-");
	force_instant_terrain_redraw();
	pause(20);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	reset_dialog();
	message_dialog("Kharprev laughs again, cutting you off and shaking the floor of the temple.","");
	pause(10);
	text_bubble_on_char(6, "PITIFUL!!");
	force_instant_terrain_redraw();
	pause(15);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	text_bubble_on_char(6, "YOU ARE NOT WORTH MY TIME!!");
	force_instant_terrain_redraw();
	pause(20);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	put_effect_on_char(6,1,15,1);
	run_animation_sound(10);
	erase_char(6);
	pause(5);
	reset_dialog();
	message_dialog("Kharprev flaps his wings once and lifts into the air. He clearly doesn't think anything of you, but you need to hold his attention until Brother Oakleaf can free Ephesos.","Not knowing what else to do, you use the Skyshard and follow him into the air...");
	play_sound(163);
	reset_dialog();
	message_dialog("Meanwhile...","");
	block_entry(1);
	set_state_continue(30);

break;

beginstate 30; //Oakleaf finds Ephesos
	force_view_center(28,41);
	set_total_visibility(1);
	force_instant_terrain_redraw();
	pause(2);
	relocate_character(9, 27, 41);
	force_view_center(27,41);
	force_instant_terrain_redraw();
	play_sound(50);

	//door #1
	pause(5);
	set_character_facing(9,0);
	force_instant_terrain_redraw();
	pause(5);
	set_character_pose(9,1);
	force_instant_terrain_redraw();
	put_effect_on_char(9,0,10,1);
	run_animation_sound(11);
	pause(5);
	put_jagged_zap(27,41,27,40,1);
	put_boom_on_space(27,40,3,0);
	run_animation_sound(51);
	pause(2);
	flip_terrain(27,39);
	play_sound(160);
	force_instant_terrain_redraw();
	pause(5);
	text_bubble_on_char(9, "Not there...");
	set_character_facing(9,2);
	set_character_pose(9,0);
	force_instant_terrain_redraw();

	//walk to # 2
	pause(10);
	relocate_character(9, 26, 41);
	force_view_center(26,41);
	force_instant_terrain_redraw();
	play_sound(49);
	pause(2);
	relocate_character(9, 25, 41);
	force_view_center(25,41);
	force_instant_terrain_redraw();
	play_sound(50);
	erase_text_bubbles();
	pause(2);
	relocate_character(9, 24, 41);
	force_view_center(24,41);
	force_instant_terrain_redraw();
	play_sound(49);


	//door #2
	pause(2);
	set_character_facing(9,0);
	force_instant_terrain_redraw();
	pause(5);
	set_character_pose(9,1);
	force_instant_terrain_redraw();
	put_effect_on_char(9,0,10,1);
	run_animation_sound(11);
	pause(5);
	put_jagged_zap(24,41,24,40,1);
	put_boom_on_space(24,40,3,0);
	run_animation_sound(51);
	pause(2);
	flip_terrain(24,39);
	play_sound(160);
	force_instant_terrain_redraw();
	pause(5);
	text_bubble_on_char(9, "Not there either...");
	set_character_facing(9,2);
	set_character_pose(9,0);
	force_instant_terrain_redraw();
	
	//on to #3
	pause(10);
	relocate_character(9, 23, 41);
	force_view_center(23,41);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(2);
	relocate_character(9, 22, 41);
	force_view_center(22,41);
	force_instant_terrain_redraw();
	play_sound(49);
	erase_text_bubbles();
	pause(2);
	relocate_character(9, 21, 41);
	force_view_center(21,41);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(2);
	relocate_character(9, 20, 41);
	force_view_center(20,41);
	force_instant_terrain_redraw();
	play_sound(49);

	//door # 3
	pause(2);
	set_character_facing(9,0);
	text_bubble_on_char(9, "He must be here...");
	force_instant_terrain_redraw();
	pause(15);
	set_character_pose(9,1);
	force_instant_terrain_redraw();
	erase_text_bubbles();
	force_instant_terrain_redraw();
	put_effect_on_char(9,0,10,1);
	run_animation_sound(11);
	pause(5);
	put_jagged_zap(20,41,20,40,1);
	put_boom_on_space(20,40,3,0);
	run_animation_sound(51);
	pause(2);
	flip_terrain(20,39);
	play_sound(160);
	set_character_pose(9,0);
	force_instant_terrain_redraw();
	pause(5);
	text_bubble_on_char(8, "Who's there? Kharprev?");
	force_instant_terrain_redraw();
	pause(15);
	erase_text_bubbles();
	text_bubble_on_char(9, "Ephesos!");
	force_instant_terrain_redraw();
	pause(10);
	relocate_character(9, 20, 40);
	force_view_center(20,40);
	force_instant_terrain_redraw();
	play_sound(50);
	pause(1);
	relocate_character(9, 20, 39);
	force_view_center(20,39);
	force_instant_terrain_redraw();
	play_sound(49);
	pause(1);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	text_bubble_on_char(8, "How...");
	force_instant_terrain_redraw();
	pause(10);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	text_bubble_on_char(9, "There's no time!");
	force_instant_terrain_redraw();
	pause(15);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	text_bubble_on_char(9, "We have friends to rescue!");
	force_instant_terrain_redraw();
	pause(20);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	
	


	set_total_visibility(0);
	move_to_new_town(11,16,25);
break;